package com.totobox.apps.activity;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.totobox.apps.R;
import com.totobox.apps.common.MoveActivity;
import com.totobox.apps.common.TotoIFData;
import com.totobox.apps.common.Util;

public class TotoNoticeListActivity extends BasicActivity {
	
	public List<Map<String,String>> noticeList;
	
	public ListView notice;
	public NoticeListAdapter listAdapter;
	
	private ProgressDialog progressDialog;
	final public int PROGRESS_DIALOG = 0;
	public Thread dataThread = null;
	public Handler handler = new Handler();
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.toto_notice_list);
        
        super.initTitle();
        
        //하단 메뉴 세팅
        super.setFooterMenu(this);
        
        notice = (ListView)findViewById(R.id.notice_list);
        notice.setOnItemClickListener(this);
    	
    	
        showDialog(PROGRESS_DIALOG);
		
		dataThread = new Thread(null, jsonList);
    	dataThread.start();
    	
    }

	@Override
	public void onClick(View v) {
		if(v == btn_title_left){
			finish();
		}
	}
    
	/**
	 * 다운로드 파일 리스트
	 */
	private Runnable jsonList = new Runnable() {
    	public void run() {
    		try {
    			
    			noticeList = TotoIFData.NoticeList("0","0","2","20","android");
		    	handler.post(dataResults);
		    	
    		} catch (Exception e) {
    			Log.d("partData : ", e.toString());
    		}
    	}
    };
    
    /**
     * json 데이터
     */
	private Runnable dataResults = new Runnable() {
    	public void run() {
    		try {
    			if (noticeList != null && !noticeList.isEmpty()) {
    				listAdapter = new NoticeListAdapter(TotoNoticeListActivity.this , R.layout.item_notice_list ,  noticeList);
    				notice.setAdapter(listAdapter);
    			}
    			else{
    				showNotFoundData();
    			}
    			
    	    	progressDialog.dismiss();
    	    	TotoNoticeListActivity.this.removeDialog(PROGRESS_DIALOG);
    	    	
    		} catch (Exception e) {
    			Log.e("xml-data", e.toString());
    		}

    	}
    };
    
    private void showNotFoundData() {
    	Toast toast = Toast.makeText(this, "다운로드 파일이 없습니다.", Toast.LENGTH_SHORT);
    	toast.show();
    }
    
    class NoticeListAdapter extends ArrayAdapter {
    	
    	private int layout;
    	private Activity context;
    	List<Map<String,String>> list;
    	
    	@SuppressWarnings("unchecked")
    	public NoticeListAdapter(Activity context , int layout , List<Map<String,String>> list){
    		super(context , layout , list);
    		this.context = context;
    		this.list = list;
    		this.layout = layout;
    	}
    	
    	public View getView(int position , View convertView , ViewGroup parent){
    		
    		Map<String,String> map = list.get(position);
    		LayoutInflater inflater = context.getLayoutInflater();
    		View view = inflater.inflate(layout, null);
    		
    		ImageView image_new = (ImageView)view.findViewById(R.id.image_new);
    		
    		TextView txt_title = (TextView)view.findViewById(R.id.txt_title);
    		TextView txt_regdt = (TextView)view.findViewById(R.id.txt_regdt);
    		txt_title.setText(map.get("vcTitle"));
    		
    		String regDate = map.get("dtRegDate");
    		regDate = Util.trim(regDate.substring(0,10)).replaceAll("-", "/");
    		
    		if(Util.getDateDiff(regDate.replaceAll("/", "")+"00")>1){
    			image_new.setVisibility(View.INVISIBLE);
    		}
    		
    		txt_regdt.setText(regDate);
    		
    		return  view;
    	}
    }
    

	@Override
	public void onItemClick(AdapterView<?> adapter, View v, int position,long id) {

		Map<String,String> map = noticeList.get(position);
		
		MoveActivity go = new MoveActivity(TotoNoticeListActivity.this , TotoNoticeDetailActivity.class);
		go.putExtra("biCursorSeq", map.get("biCursorSeq"));
		go.putExtra("vcMemberID", map.get("vcMemberID"));
		go.putExtra("vcTitle", map.get("vcTitle"));
		go.putExtra("vcContent", map.get("vcContent"));
		go.putExtra("dtRegDate", map.get("dtRegDate"));
		go.putExtra("iReadCount", map.get("iReadCount"));
		
		go.startActivity();
		
	}

	@Override
	protected Dialog onCreateDialog (int id) {
		switch (id) {
		case PROGRESS_DIALOG:
			
			progressDialog = new ProgressDialog(this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage("목록 로딩중 입니다.");
			return progressDialog;
		default:
			
			return null;
		}
	}
}